<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div>
            <el-row>
              <el-button type="primary" size="small" v-on:click="fetchData">刷新</el-button>
            </el-row>
            <el-table
              :data="taskList"
              stripe
              highlight-current-row
              v-loading="loading"
              size="mini"
              height="720"
              @row-dblclick="edit"
              style="width: 100%">
              <el-table-column
                prop="cronKey"
                width="460"
                label="定时任务完整类名"
              >
              </el-table-column>
              <el-table-column
                prop="cronExpression"
                label="cron表达式"
              >
              </el-table-column>

              <el-table-column
                prop="taskExplain"
                label="任务描述">
              </el-table-column>

              <el-table-column
                label="状态">
                <template slot-scope="scope">
                  <el-switch
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    :active-value="0"
                    :inactive-value="1"
                    @change="save(scope.row)"
                    v-model="scope.row.status"/>
                </template>
              </el-table-column>

            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="编辑" :visible.sync="dialogVisible">
      <el-form :model="current_row" label-position="right" label-width="200px" size="mini">
        <el-form-item label="定时任务完整类名">
          <el-input v-model="current_row.cronKey" autocomplete="off" style="width:400px"></el-input>
        </el-form-item>
        <el-form-item label="cron表达式">
          <el-input v-model="current_row.cronExpression" autocomplete="off" style="width:400px"></el-input>
        </el-form-item>
        <el-form-item label="任务描述">
          <el-input v-model="current_row.taskExplain" autocomplete="off" style="width:400px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save(current_row)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { obj } from '@/api/scheduledOfTask'

export default {
  data() {
    return {
      taskList: [],
      dialogVisible: false,
      current_row: new Object()
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      obj.getTaskList().then(response => {
        this.taskList = response.data
        this.loading = false
      })
    },
    save(row) {
      const _this = this
      obj.saveTask(row).then(resp => {
        this.dialogVisible = false
        _this.$message({
          type: 'success',
          message: '保存成功!'
        })
      })
    },
    edit(row) {
      this.current_row = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.box-card .el-row {
  padding: 5px 20px 20px 20px;
}


</style>

